home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume22 / gawk2.11 / part14 < prev    next >
Encoding:
Internet Message Format  |  1990-06-07  |  51.8 KB

  1. Subject:  v22i100:  GNU AWK, version 2.11, Part14/16
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 9ac281a3 0c7e0e8f b35bf3a3 fe73e35f
  5.  
  6. Submitted-by: "Arnold D. Robbins" <arnold@unix.cc.emory.edu>
  7. Posting-number: Volume 22, Issue 100
  8. Archive-name: gawk2.11/part14
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then feed it
  12. # into a shell via "sh file" or similar.  To overwrite existing files,
  13. # type "sh file -c".
  14. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  15. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  16. # Contents:  ./COPYING ./debug.c ./main.c ./regex.h
  17. # Wrapped by rsalz@litchi.bbn.com on Wed Jun  6 12:25:00 1990
  18. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  19. echo If this archive is complete, you will see the following message:
  20. echo '          "shar: End of archive 14 (of 16)."'
  21. if test -f './COPYING' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'./COPYING'\"
  23. else
  24.   echo shar: Extracting \"'./COPYING'\" \(12488 characters\)
  25.   sed "s/^X//" >'./COPYING' <<'END_OF_FILE'
  26. X
  27. X            GNU GENERAL PUBLIC LICENSE
  28. X             Version 1, February 1989
  29. X
  30. X Copyright (C) 1989 Free Software Foundation, Inc.
  31. X                    675 Mass Ave, Cambridge, MA 02139, USA
  32. X Everyone is permitted to copy and distribute verbatim copies
  33. X of this license document, but changing it is not allowed.
  34. X
  35. X                Preamble
  36. X
  37. X  The license agreements of most software companies try to keep users
  38. Xat the mercy of those companies.  By contrast, our General Public
  39. XLicense is intended to guarantee your freedom to share and change free
  40. Xsoftware--to make sure the software is free for all its users.  The
  41. XGeneral Public License applies to the Free Software Foundation's
  42. Xsoftware and to any other program whose authors commit to using it.
  43. XYou can use it for your programs, too.
  44. X
  45. X  When we speak of free software, we are referring to freedom, not
  46. Xprice.  Specifically, the General Public License is designed to make
  47. Xsure that you have the freedom to give away or sell copies of free
  48. Xsoftware, that you receive source code or can get it if you want it,
  49. Xthat you can change the software or use pieces of it in new free
  50. Xprograms; and that you know you can do these things.
  51. X
  52. X  To protect your rights, we need to make restrictions that forbid
  53. Xanyone to deny you these rights or to ask you to surrender the rights.
  54. XThese restrictions translate to certain responsibilities for you if you
  55. Xdistribute copies of the software, or if you modify it.
  56. X
  57. X  For example, if you distribute copies of a such a program, whether
  58. Xgratis or for a fee, you must give the recipients all the rights that
  59. Xyou have.  You must make sure that they, too, receive or can get the
  60. Xsource code.  And you must tell them their rights.
  61. X
  62. X  We protect your rights with two steps: (1) copyright the software, and
  63. X(2) offer you this license which gives you legal permission to copy,
  64. Xdistribute and/or modify the software.
  65. X
  66. X  Also, for each author's protection and ours, we want to make certain
  67. Xthat everyone understands that there is no warranty for this free
  68. Xsoftware.  If the software is modified by someone else and passed on, we
  69. Xwant its recipients to know that what they have is not the original, so
  70. Xthat any problems introduced by others will not reflect on the original
  71. Xauthors' reputations.
  72. X
  73. X  The precise terms and conditions for copying, distribution and
  74. Xmodification follow.
  75. X
  76. X            GNU GENERAL PUBLIC LICENSE
  77. X   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  78. X
  79. X  0. This License Agreement applies to any program or other work which
  80. Xcontains a notice placed by the copyright holder saying it may be
  81. Xdistributed under the terms of this General Public License.  The
  82. X"Program", below, refers to any such program or work, and a "work based
  83. Xon the Program" means either the Program or any work containing the
  84. XProgram or a portion of it, either verbatim or with modifications.  Each
  85. Xlicensee is addressed as "you".
  86. X
  87. X  1. You may copy and distribute verbatim copies of the Program's source
  88. Xcode as you receive it, in any medium, provided that you conspicuously and
  89. Xappropriately publish on each copy an appropriate copyright notice and
  90. Xdisclaimer of warranty; keep intact all the notices that refer to this
  91. XGeneral Public License and to the absence of any warranty; and give any
  92. Xother recipients of the Program a copy of this General Public License
  93. Xalong with the Program.  You may charge a fee for the physical act of
  94. Xtransferring a copy.
  95. X
  96. X  2. You may modify your copy or copies of the Program or any portion of
  97. Xit, and copy and distribute such modifications under the terms of Paragraph
  98. X1 above, provided that you also do the following:
  99. X
  100. X    a) cause the modified files to carry prominent notices stating that
  101. X    you changed the files and the date of any change; and
  102. X
  103. X    b) cause the whole of any work that you distribute or publish, that
  104. X    in whole or in part contains the Program or any part thereof, either
  105. X    with or without modifications, to be licensed at no charge to all
  106. X    third parties under the terms of this General Public License (except
  107. X    that you may choose to grant warranty protection to some or all
  108. X    third parties, at your option).
  109. X
  110. X    c) If the modified program normally reads commands interactively when
  111. X    run, you must cause it, when started running for such interactive use
  112. X    in the simplest and most usual way, to print or display an
  113. X    announcement including an appropriate copyright notice and a notice
  114. X    that there is no warranty (or else, saying that you provide a
  115. X    warranty) and that users may redistribute the program under these
  116. X    conditions, and telling the user how to view a copy of this General
  117. X    Public License.
  118. X
  119. X    d) You may charge a fee for the physical act of transferring a
  120. X    copy, and you may at your option offer warranty protection in
  121. X    exchange for a fee.
  122. X
  123. XMere aggregation of another independent work with the Program (or its
  124. Xderivative) on a volume of a storage or distribution medium does not bring
  125. Xthe other work under the scope of these terms.
  126. X
  127. X  3. You may copy and distribute the Program (or a portion or derivative of
  128. Xit, under Paragraph 2) in object code or executable form under the terms of
  129. XParagraphs 1 and 2 above provided that you also do one of the following:
  130. X
  131. X    a) accompany it with the complete corresponding machine-readable
  132. X    source code, which must be distributed under the terms of
  133. X    Paragraphs 1 and 2 above; or,
  134. X
  135. X    b) accompany it with a written offer, valid for at least three
  136. X    years, to give any third party free (except for a nominal charge
  137. X    for the cost of distribution) a complete machine-readable copy of the
  138. X    corresponding source code, to be distributed under the terms of
  139. X    Paragraphs 1 and 2 above; or,
  140. X
  141. X    c) accompany it with the information you received as to where the
  142. X    corresponding source code may be obtained.  (This alternative is
  143. X    allowed only for noncommercial distribution and only if you
  144. X    received the program in object code or executable form alone.)
  145. X
  146. XSource code for a work means the preferred form of the work for making
  147. Xmodifications to it.  For an executable file, complete source code means
  148. Xall the source code for all modules it contains; but, as a special
  149. Xexception, it need not include source code for modules which are standard
  150. Xlibraries that accompany the operating system on which the executable
  151. Xfile runs, or for standard header files or definitions files that
  152. Xaccompany that operating system.
  153. X
  154. X  4. You may not copy, modify, sublicense, distribute or transfer the
  155. XProgram except as expressly provided under this General Public License.
  156. XAny attempt otherwise to copy, modify, sublicense, distribute or transfer
  157. Xthe Program is void, and will automatically terminate your rights to use
  158. Xthe Program under this License.  However, parties who have received
  159. Xcopies, or rights to use copies, from you under this General Public
  160. XLicense will not have their licenses terminated so long as such parties
  161. Xremain in full compliance.
  162. X
  163. X  5. By copying, distributing or modifying the Program (or any work based
  164. Xon the Program) you indicate your acceptance of this license to do so,
  165. Xand all its terms and conditions.
  166. X
  167. X  6. Each time you redistribute the Program (or any work based on the
  168. XProgram), the recipient automatically receives a license from the original
  169. Xlicensor to copy, distribute or modify the Program subject to these
  170. Xterms and conditions.  You may not impose any further restrictions on the
  171. Xrecipients' exercise of the rights granted herein.
  172. X
  173. X  7. The Free Software Foundation may publish revised and/or new versions
  174. Xof the General Public License from time to time.  Such new versions will
  175. Xbe similar in spirit to the present version, but may differ in detail to
  176. Xaddress new problems or concerns.
  177. X
  178. XEach version is given a distinguishing version number.  If the Program
  179. Xspecifies a version number of the license which applies to it and "any
  180. Xlater version", you have the option of following the terms and conditions
  181. Xeither of that version or of any later version published by the Free
  182. XSoftware Foundation.  If the Program does not specify a version number of
  183. Xthe license, you may choose any version ever published by the Free Software
  184. XFoundation.
  185. X
  186. X  8. If you wish to incorporate parts of the Program into other free
  187. Xprograms whose distribution conditions are different, write to the author
  188. Xto ask for permission.  For software which is copyrighted by the Free
  189. XSoftware Foundation, write to the Free Software Foundation; we sometimes
  190. Xmake exceptions for this.  Our decision will be guided by the two goals
  191. Xof preserving the free status of all derivatives of our free software and
  192. Xof promoting the sharing and reuse of software generally.
  193. X
  194. X                NO WARRANTY
  195. X
  196. X  9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  197. XFOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
  198. XOTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  199. XPROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
  200. XOR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  201. XMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
  202. XTO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  203. XPROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
  204. XREPAIR OR CORRECTION.
  205. X
  206. X  10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  207. XWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
  208. XREDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
  209. XINCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
  210. XOUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
  211. XTO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  212. XYOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
  213. XPROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
  214. XPOSSIBILITY OF SUCH DAMAGES.
  215. X
  216. X             END OF TERMS AND CONDITIONS
  217. X
  218. X    Appendix: How to Apply These Terms to Your New Programs
  219. X
  220. X  If you develop a new program, and you want it to be of the greatest
  221. Xpossible use to humanity, the best way to achieve this is to make it
  222. Xfree software which everyone can redistribute and change under these
  223. Xterms.
  224. X
  225. X  To do so, attach the following notices to the program.  It is safest to
  226. Xattach them to the start of each source file to most effectively convey
  227. Xthe exclusion of warranty; and each file should have at least the
  228. X"copyright" line and a pointer to where the full notice is found.
  229. X
  230. X    <one line to give the program's name and a brief idea of what it does.>
  231. X    Copyright (C) 19yy  <name of author>
  232. X
  233. X    This program is free software; you can redistribute it and/or modify
  234. X    it under the terms of the GNU General Public License as published by
  235. X    the Free Software Foundation; either version 1, or (at your option)
  236. X    any later version.
  237. X
  238. X    This program is distributed in the hope that it will be useful,
  239. X    but WITHOUT ANY WARRANTY; without even the implied warranty of
  240. X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  241. X    GNU General Public License for more details.
  242. X
  243. X    You should have received a copy of the GNU General Public License
  244. X    along with this program; if not, write to the Free Software
  245. X    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  246. X
  247. XAlso add information on how to contact you by electronic and paper mail.
  248. X
  249. XIf the program is interactive, make it output a short notice like this
  250. Xwhen it starts in an interactive mode:
  251. X
  252. X    Gnomovision version 69, Copyright (C) 19xx name of author
  253. X    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  254. X    This is free software, and you are welcome to redistribute it
  255. X    under certain conditions; type `show c' for details.
  256. X
  257. XThe hypothetical commands `show w' and `show c' should show the
  258. Xappropriate parts of the General Public License.  Of course, the
  259. Xcommands you use may be called something other than `show w' and `show
  260. Xc'; they could even be mouse-clicks or menu items--whatever suits your
  261. Xprogram.
  262. X
  263. XYou should also get your employer (if you work as a programmer) or your
  264. Xschool, if any, to sign a "copyright disclaimer" for the program, if
  265. Xnecessary.  Here a sample; alter the names:
  266. X
  267. X  Yoyodyne, Inc., hereby disclaims all copyright interest in the
  268. X  program `Gnomovision' (a program to direct compilers to make passes
  269. X  at assemblers) written by James Hacker.
  270. X
  271. X  <signature of Ty Coon>, 1 April 1989
  272. X  Ty Coon, President of Vice
  273. X
  274. XThat's all there is to it!
  275. END_OF_FILE
  276.   if test 12488 -ne `wc -c <'./COPYING'`; then
  277.     echo shar: \"'./COPYING'\" unpacked with wrong size!
  278.   fi
  279.   # end of './COPYING'
  280. fi
  281. if test -f './debug.c' -a "${1}" != "-c" ; then 
  282.   echo shar: Will not clobber existing file \"'./debug.c'\"
  283. else
  284.   echo shar: Extracting \"'./debug.c'\" \(10721 characters\)
  285.   sed "s/^X//" >'./debug.c' <<'END_OF_FILE'
  286. X/*
  287. X * debug.c -- Various debugging routines 
  288. X */
  289. X
  290. X/* 
  291. X * Copyright (C) 1986, 1988, 1989 the Free Software Foundation, Inc.
  292. X * 
  293. X * This file is part of GAWK, the GNU implementation of the
  294. X * AWK Progamming Language.
  295. X * 
  296. X * GAWK is free software; you can redistribute it and/or modify
  297. X * it under the terms of the GNU General Public License as published by
  298. X * the Free Software Foundation; either version 1, or (at your option)
  299. X * any later version.
  300. X * 
  301. X * GAWK is distributed in the hope that it will be useful,
  302. X * but WITHOUT ANY WARRANTY; without even the implied warranty of
  303. X * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  304. X * GNU General Public License for more details.
  305. X * 
  306. X * You should have received a copy of the GNU General Public License
  307. X * along with GAWK; see the file COPYING.  If not, write to
  308. X * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  309. X */
  310. X
  311. X#include "awk.h"
  312. X
  313. X#ifdef DEBUG
  314. X
  315. Xextern NODE **fields_arr;
  316. X
  317. X
  318. X/* This is all debugging stuff.  Ignore it and maybe it'll go away. */
  319. X
  320. X/*
  321. X * Some of it could be turned into a really cute trace command, if anyone
  322. X * wants to.  
  323. X */
  324. Xchar *nnames[] = {
  325. X    "illegal", "times", "quotient", "mod", "plus",
  326. X    "minus", "cond_pair", "subscript", "concat", "exp",
  327. X    /* 10 */
  328. X    "preincrement", "predecrement", "postincrement", "postdecrement",
  329. X    "unary_minus",
  330. X    "field_spec", "assign", "assign_times", "assign_quotient", "assign_mod",
  331. X    /* 20 */
  332. X    "assign_plus", "assign_minus", "assign_exp", "and", "or",
  333. X    "equal", "notequal", "less", "greater", "leq",
  334. X    /* 30 */
  335. X    "geq", "match", "nomatch", "not", "rule_list",
  336. X    "rule_node", "statement_list", "if_branches", "expression_list",
  337. X    "param_list",
  338. X    /* 40 */
  339. X    "K_if", "K_while", "K_for", "K_arrayfor", "K_break",
  340. X    "K_continue", "K_print", "K_printf", "K_next", "K_exit",
  341. X    /* 50 */
  342. X    "K_do", "K_return", "K_delete", "K_getline", "K_function",
  343. X    "redirect_output", "redirect_append", "redirect_pipe",
  344. X    "redirect_pipein", "redirect_input",
  345. X    /* 60 */
  346. X    "var", "var_array", "val", "builtin", "line_range",
  347. X    "in_array", "func", "func_call", "cond_exp", "regex",
  348. X    /* 70 */
  349. X    "hashnode", "ahash"
  350. X};
  351. X
  352. Xptree(n)
  353. XNODE *n;
  354. X{
  355. X    print_parse_tree(n);
  356. X}
  357. X
  358. Xpt()
  359. X{
  360. X    long x;
  361. X
  362. X    (void) scanf("%x", &x);
  363. X    printf("0x%x\n", x);
  364. X    print_parse_tree((NODE *) x);
  365. X    fflush(stdout);
  366. X}
  367. X
  368. Xstatic depth = 0;
  369. X
  370. Xprint_parse_tree(ptr)
  371. XNODE *ptr;
  372. X{
  373. X    if (!ptr) {
  374. X        printf("NULL\n");
  375. X        return;
  376. X    }
  377. X    if ((int) (ptr->type) < 0 || (int) (ptr->type) > sizeof(nnames) / sizeof(nnames[0])) {
  378. X        printf("(0x%x Type %d??)\n", ptr, ptr->type);
  379. X        return;
  380. X    }
  381. X    printf("(%d)%*s", depth, depth, "");
  382. X    switch ((int) ptr->type) {
  383. X    case (int) Node_val:
  384. X        printf("(0x%x Value ", ptr);
  385. X        if (ptr->flags&STR)
  386. X            printf("str: \"%.*s\" ", ptr->stlen, ptr->stptr);
  387. X        if (ptr->flags&NUM)
  388. X            printf("num: %g", ptr->numbr);
  389. X        printf(")\n");
  390. X        return;
  391. X    case (int) Node_var_array:
  392. X        {
  393. X        struct search *l;
  394. X
  395. X        printf("(0x%x Array)\n", ptr);
  396. X        for (l = assoc_scan(ptr); l; l = assoc_next(l)) {
  397. X            printf("\tindex: ");
  398. X            print_parse_tree(l->retval);
  399. X            printf("\tvalue: ");
  400. X            print_parse_tree(*assoc_lookup(ptr, l->retval));
  401. X            printf("\n");
  402. X        }
  403. X        return;
  404. X        }
  405. X    case Node_param_list:
  406. X        printf("(0x%x Local variable %s)\n", ptr, ptr->param);
  407. X        if (ptr->rnode)
  408. X            print_parse_tree(ptr->rnode);
  409. X        return;
  410. X    case Node_regex:
  411. X        printf("(0x%x Regular expression %s\n", ptr, ptr->re_text);
  412. X        return;
  413. X    }
  414. X    if (ptr->lnode)
  415. X        printf("0x%x = left<--", ptr->lnode);
  416. X    printf("(0x%x %s.%d)", ptr, nnames[(int) (ptr->type)], ptr->type);
  417. X    if (ptr->rnode)
  418. X        printf("-->right = 0x%x", ptr->rnode);
  419. X    printf("\n");
  420. X    depth++;
  421. X    if (ptr->lnode)
  422. X        print_parse_tree(ptr->lnode);
  423. X    switch ((int) ptr->type) {
  424. X    case (int) Node_line_range:
  425. X    case (int) Node_match:
  426. X    case (int) Node_nomatch:
  427. X        break;
  428. X    case (int) Node_builtin:
  429. X        printf("Builtin: %d\n", ptr->proc);
  430. X        break;
  431. X    case (int) Node_K_for:
  432. X    case (int) Node_K_arrayfor:
  433. X        printf("(%s:)\n", nnames[(int) (ptr->type)]);
  434. X        print_parse_tree(ptr->forloop->init);
  435. X        printf("looping:\n");
  436. X        print_parse_tree(ptr->forloop->cond);
  437. X        printf("doing:\n");
  438. X        print_parse_tree(ptr->forloop->incr);
  439. X        break;
  440. X    default:
  441. X        if (ptr->rnode)
  442. X            print_parse_tree(ptr->rnode);
  443. X        break;
  444. X    }
  445. X    --depth;
  446. X}
  447. X
  448. X
  449. X/*
  450. X * print out all the variables in the world 
  451. X */
  452. X
  453. Xdump_vars()
  454. X{
  455. X    register int n;
  456. X    register NODE *buc;
  457. X
  458. X#ifdef notdef
  459. X    printf("Fields:");
  460. X    dump_fields();
  461. X#endif
  462. X    printf("Vars:\n");
  463. X    for (n = 0; n < HASHSIZE; n++) {
  464. X        for (buc = variables[n]; buc; buc = buc->hnext) {
  465. X            printf("'%.*s': ", buc->hlength, buc->hname);
  466. X            print_parse_tree(buc->hvalue);
  467. X        }
  468. X    }
  469. X    printf("End\n");
  470. X}
  471. X
  472. X#ifdef notdef
  473. Xdump_fields()
  474. X{
  475. X    register NODE **p;
  476. X    register int n;
  477. X
  478. X    printf("%d fields\n", f_arr_siz);
  479. X    for (n = 0, p = &fields_arr[0]; n < f_arr_siz; n++, p++) {
  480. X        printf("$%d is '", n);
  481. X        print_simple(*p, stdout);
  482. X        printf("'\n");
  483. X    }
  484. X}
  485. X#endif
  486. X
  487. X/* VARARGS1 */
  488. Xprint_debug(str, n)
  489. Xchar *str;
  490. X{
  491. X    extern int debugging;
  492. X
  493. X    if (debugging)
  494. X        printf("%s:0x%x\n", str, n);
  495. X}
  496. X
  497. Xint indent = 0;
  498. X
  499. Xprint_a_node(ptr)
  500. XNODE *ptr;
  501. X{
  502. X    NODE *p1;
  503. X    char *str, *str2;
  504. X    int n;
  505. X    NODE *buc;
  506. X
  507. X    if (!ptr)
  508. X        return;        /* don't print null ptrs */
  509. X    switch (ptr->type) {
  510. X    case Node_val:
  511. X        if (ptr->flags&NUM)
  512. X            printf("%g", ptr->numbr);
  513. X        else
  514. X            printf("\"%.*s\"", ptr->stlen, ptr->stptr);
  515. X        return;
  516. X    case Node_times:
  517. X        str = "*";
  518. X        goto pr_twoop;
  519. X    case Node_quotient:
  520. X        str = "/";
  521. X        goto pr_twoop;
  522. X    case Node_mod:
  523. X        str = "%";
  524. X        goto pr_twoop;
  525. X    case Node_plus:
  526. X        str = "+";
  527. X        goto pr_twoop;
  528. X    case Node_minus:
  529. X        str = "-";
  530. X        goto pr_twoop;
  531. X    case Node_exp:
  532. X        str = "^";
  533. X        goto pr_twoop;
  534. X    case Node_concat:
  535. X        str = " ";
  536. X        goto pr_twoop;
  537. X    case Node_assign:
  538. X        str = "=";
  539. X        goto pr_twoop;
  540. X    case Node_assign_times:
  541. X        str = "*=";
  542. X        goto pr_twoop;
  543. X    case Node_assign_quotient:
  544. X        str = "/=";
  545. X        goto pr_twoop;
  546. X    case Node_assign_mod:
  547. X        str = "%=";
  548. X        goto pr_twoop;
  549. X    case Node_assign_plus:
  550. X        str = "+=";
  551. X        goto pr_twoop;
  552. X    case Node_assign_minus:
  553. X        str = "-=";
  554. X        goto pr_twoop;
  555. X    case Node_assign_exp:
  556. X        str = "^=";
  557. X        goto pr_twoop;
  558. X    case Node_and:
  559. X        str = "&&";
  560. X        goto pr_twoop;
  561. X    case Node_or:
  562. X        str = "||";
  563. X        goto pr_twoop;
  564. X    case Node_equal:
  565. X        str = "==";
  566. X        goto pr_twoop;
  567. X    case Node_notequal:
  568. X        str = "!=";
  569. X        goto pr_twoop;
  570. X    case Node_less:
  571. X        str = "<";
  572. X        goto pr_twoop;
  573. X    case Node_greater:
  574. X        str = ">";
  575. X        goto pr_twoop;
  576. X    case Node_leq:
  577. X        str = "<=";
  578. X        goto pr_twoop;
  579. X    case Node_geq:
  580. X        str = ">=";
  581. X        goto pr_twoop;
  582. X
  583. Xpr_twoop:
  584. X        print_a_node(ptr->lnode);
  585. X        printf("%s", str);
  586. X        print_a_node(ptr->rnode);
  587. X        return;
  588. X
  589. X    case Node_not:
  590. X        str = "!";
  591. X        str2 = "";
  592. X        goto pr_oneop;
  593. X    case Node_field_spec:
  594. X        str = "$(";
  595. X        str2 = ")";
  596. X        goto pr_oneop;
  597. X    case Node_postincrement:
  598. X        str = "";
  599. X        str2 = "++";
  600. X        goto pr_oneop;
  601. X    case Node_postdecrement:
  602. X        str = "";
  603. X        str2 = "--";
  604. X        goto pr_oneop;
  605. X    case Node_preincrement:
  606. X        str = "++";
  607. X        str2 = "";
  608. X        goto pr_oneop;
  609. X    case Node_predecrement:
  610. X        str = "--";
  611. X        str2 = "";
  612. X        goto pr_oneop;
  613. Xpr_oneop:
  614. X        printf(str);
  615. X        print_a_node(ptr->subnode);
  616. X        printf(str2);
  617. X        return;
  618. X
  619. X    case Node_expression_list:
  620. X        print_a_node(ptr->lnode);
  621. X        if (ptr->rnode) {
  622. X            printf(",");
  623. X            print_a_node(ptr->rnode);
  624. X        }
  625. X        return;
  626. X
  627. X    case Node_var:
  628. X        for (n = 0; n < HASHSIZE; n++) {
  629. X            for (buc = variables[n]; buc; buc = buc->hnext) {
  630. X                if (buc->hvalue == ptr) {
  631. X                    printf("%.*s", buc->hlength, buc->hname);
  632. X                    n = HASHSIZE;
  633. X                    break;
  634. X                }
  635. X            }
  636. X        }
  637. X        return;
  638. X    case Node_subscript:
  639. X        print_a_node(ptr->lnode);
  640. X        printf("[");
  641. X        print_a_node(ptr->rnode);
  642. X        printf("]");
  643. X        return;
  644. X    case Node_builtin:
  645. X        printf("some_builtin(");
  646. X        print_a_node(ptr->subnode);
  647. X        printf(")");
  648. X        return;
  649. X
  650. X    case Node_statement_list:
  651. X        printf("{\n");
  652. X        indent++;
  653. X        for (n = indent; n; --n)
  654. X            printf("  ");
  655. X        while (ptr) {
  656. X            print_maybe_semi(ptr->lnode);
  657. X            if (ptr->rnode)
  658. X                for (n = indent; n; --n)
  659. X                    printf("  ");
  660. X            ptr = ptr->rnode;
  661. X        }
  662. X        --indent;
  663. X        for (n = indent; n; --n)
  664. X            printf("  ");
  665. X        printf("}\n");
  666. X        for (n = indent; n; --n)
  667. X            printf("  ");
  668. X        return;
  669. X
  670. X    case Node_K_if:
  671. X        printf("if(");
  672. X        print_a_node(ptr->lnode);
  673. X        printf(") ");
  674. X        ptr = ptr->rnode;
  675. X        if (ptr->lnode->type == Node_statement_list) {
  676. X            printf("{\n");
  677. X            indent++;
  678. X            for (p1 = ptr->lnode; p1; p1 = p1->rnode) {
  679. X                for (n = indent; n; --n)
  680. X                    printf("  ");
  681. X                print_maybe_semi(p1->lnode);
  682. X            }
  683. X            --indent;
  684. X            for (n = indent; n; --n)
  685. X                printf("  ");
  686. X            if (ptr->rnode) {
  687. X                printf("} else ");
  688. X            } else {
  689. X                printf("}\n");
  690. X                return;
  691. X            }
  692. X        } else {
  693. X            print_maybe_semi(ptr->lnode);
  694. X            if (ptr->rnode) {
  695. X                for (n = indent; n; --n)
  696. X                    printf("  ");
  697. X                printf("else ");
  698. X            } else
  699. X                return;
  700. X        }
  701. X        if (!ptr->rnode)
  702. X            return;
  703. X        deal_with_curls(ptr->rnode);
  704. X        return;
  705. X
  706. X    case Node_K_while:
  707. X        printf("while(");
  708. X        print_a_node(ptr->lnode);
  709. X        printf(") ");
  710. X        deal_with_curls(ptr->rnode);
  711. X        return;
  712. X
  713. X    case Node_K_do:
  714. X        printf("do ");
  715. X        deal_with_curls(ptr->rnode);
  716. X        printf("while(");
  717. X        print_a_node(ptr->lnode);
  718. X        printf(") ");
  719. X        return;
  720. X
  721. X    case Node_K_for:
  722. X        printf("for(");
  723. X        print_a_node(ptr->forloop->init);
  724. X        printf(";");
  725. X        print_a_node(ptr->forloop->cond);
  726. X        printf(";");
  727. X        print_a_node(ptr->forloop->incr);
  728. X        printf(") ");
  729. X        deal_with_curls(ptr->forsub);
  730. X        return;
  731. X    case Node_K_arrayfor:
  732. X        printf("for(");
  733. X        print_a_node(ptr->forloop->init);
  734. X        printf(" in ");
  735. X        print_a_node(ptr->forloop->incr);
  736. X        printf(") ");
  737. X        deal_with_curls(ptr->forsub);
  738. X        return;
  739. X
  740. X    case Node_K_printf:
  741. X        printf("printf(");
  742. X        print_a_node(ptr->lnode);
  743. X        printf(")");
  744. X        return;
  745. X    case Node_K_print:
  746. X        printf("print(");
  747. X        print_a_node(ptr->lnode);
  748. X        printf(")");
  749. X        return;
  750. X    case Node_K_next:
  751. X        printf("next");
  752. X        return;
  753. X    case Node_K_break:
  754. X        printf("break");
  755. X        return;
  756. X    case Node_K_delete:
  757. X        printf("delete ");
  758. X        print_a_node(ptr->lnode);
  759. X        return;
  760. X    case Node_func:
  761. X        printf("function %s (", ptr->lnode->param);
  762. X        if (ptr->lnode->rnode)
  763. X            print_a_node(ptr->lnode->rnode);
  764. X        printf(")\n");
  765. X        print_a_node(ptr->rnode);
  766. X        return;
  767. X    case Node_param_list:
  768. X        printf("%s", ptr->param);
  769. X        if (ptr->rnode) {
  770. X            printf(", ");
  771. X            print_a_node(ptr->rnode);
  772. X        }
  773. X        return;
  774. X    default:
  775. X        print_parse_tree(ptr);
  776. X        return;
  777. X    }
  778. X}
  779. X
  780. Xprint_maybe_semi(ptr)
  781. XNODE *ptr;
  782. X{
  783. X    print_a_node(ptr);
  784. X    switch (ptr->type) {
  785. X    case Node_K_if:
  786. X    case Node_K_for:
  787. X    case Node_K_arrayfor:
  788. X    case Node_statement_list:
  789. X        break;
  790. X    default:
  791. X        printf(";\n");
  792. X        break;
  793. X    }
  794. X}
  795. X
  796. Xdeal_with_curls(ptr)
  797. XNODE *ptr;
  798. X{
  799. X    int n;
  800. X
  801. X    if (ptr->type == Node_statement_list) {
  802. X        printf("{\n");
  803. X        indent++;
  804. X        while (ptr) {
  805. X            for (n = indent; n; --n)
  806. X                printf("  ");
  807. X            print_maybe_semi(ptr->lnode);
  808. X            ptr = ptr->rnode;
  809. X        }
  810. X        --indent;
  811. X        for (n = indent; n; --n)
  812. X            printf("  ");
  813. X        printf("}\n");
  814. X    } else {
  815. X        print_maybe_semi(ptr);
  816. X    }
  817. X}
  818. X
  819. XNODE *
  820. Xdo_prvars()
  821. X{
  822. X    dump_vars();
  823. X    return Nnull_string;
  824. X}
  825. X
  826. XNODE *
  827. Xdo_bp()
  828. X{
  829. X    return Nnull_string;
  830. X}
  831. X
  832. X#endif
  833. X
  834. X#ifdef MEMDEBUG
  835. X
  836. X#undef free
  837. Xextern void free();
  838. X
  839. Xvoid
  840. Xdo_free(s)
  841. Xchar *s;
  842. X{
  843. X    free(s);
  844. X}
  845. X
  846. X#endif
  847. END_OF_FILE
  848.   if test 10721 -ne `wc -c <'./debug.c'`; then
  849.     echo shar: \"'./debug.c'\" unpacked with wrong size!
  850.   fi
  851.   # end of './debug.c'
  852. fi
  853. if test -f './main.c' -a "${1}" != "-c" ; then 
  854.   echo shar: Will not clobber existing file \"'./main.c'\"
  855. else
  856.   echo shar: Extracting \"'./main.c'\" \(12595 characters\)
  857.   sed "s/^X//" >'./main.c' <<'END_OF_FILE'
  858. X/*
  859. X * main.c -- Expression tree constructors and main program for gawk. 
  860. X */
  861. X
  862. X/* 
  863. X * Copyright (C) 1986, 1988, 1989 the Free Software Foundation, Inc.
  864. X * 
  865. X * This file is part of GAWK, the GNU implementation of the
  866. X * AWK Progamming Language.
  867. X * 
  868. X * GAWK is free software; you can redistribute it and/or modify
  869. X * it under the terms of the GNU General Public License as published by
  870. X * the Free Software Foundation; either version 1, or (at your option)
  871. X * any later version.
  872. X * 
  873. X * GAWK is distributed in the hope that it will be useful,
  874. X * but WITHOUT ANY WARRANTY; without even the implied warranty of
  875. X * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  876. X * GNU General Public License for more details.
  877. X * 
  878. X * You should have received a copy of the GNU General Public License
  879. X * along with GAWK; see the file COPYING.  If not, write to
  880. X * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  881. X */
  882. X
  883. X#include "awk.h"
  884. X#include "patchlevel.h"
  885. X#include <signal.h>
  886. X
  887. Xextern int yyparse();
  888. Xextern void do_input();
  889. Xextern int close_io();
  890. Xextern void init_fields();
  891. Xextern int getopt();
  892. Xextern int re_set_syntax();
  893. Xextern NODE *node();
  894. X
  895. Xstatic void usage();
  896. Xstatic void set_fs();
  897. Xstatic void init_vars();
  898. Xstatic void init_args();
  899. Xstatic NODE *spc_var();
  900. Xstatic void pre_assign();
  901. Xstatic void copyleft();
  902. X
  903. X/* These nodes store all the special variables AWK uses */
  904. XNODE *FS_node, *NF_node, *RS_node, *NR_node;
  905. XNODE *FILENAME_node, *OFS_node, *ORS_node, *OFMT_node;
  906. XNODE *FNR_node, *RLENGTH_node, *RSTART_node, *SUBSEP_node;
  907. XNODE *ENVIRON_node, *IGNORECASE_node;
  908. XNODE *ARGC_node, *ARGV_node;
  909. X
  910. X/*
  911. X * The parse tree and field nodes are stored here.  Parse_end is a dummy item
  912. X * used to free up unneeded fields without freeing the program being run 
  913. X */
  914. Xint errcount = 0;    /* error counter, used by yyerror() */
  915. X
  916. X/* The global null string */
  917. XNODE *Nnull_string;
  918. X
  919. X/* The name the program was invoked under, for error messages */
  920. Xchar *myname;
  921. X
  922. X/* A block of AWK code to be run before running the program */
  923. XNODE *begin_block = 0;
  924. X
  925. X/* A block of AWK code to be run after the last input file */
  926. XNODE *end_block = 0;
  927. X
  928. Xint exiting = 0;        /* Was an "exit" statement executed? */
  929. Xint exit_val = 0;        /* optional exit value */
  930. X
  931. X#ifdef DEBUG
  932. X/* non-zero means in debugging is enabled.  Probably not very useful */
  933. Xint debugging = 0;
  934. Xextern int yydebug;
  935. X#endif
  936. X
  937. Xint tempsource = 0;        /* source is in a temp file */
  938. Xchar **sourcefile = NULL;    /* source file name(s) */
  939. Xint numfiles = -1;        /* how many source files */
  940. X
  941. Xint strict = 0;            /* turn off gnu extensions */
  942. X
  943. Xint output_is_tty = 0;        /* control flushing of output */
  944. X
  945. XNODE *expression_value;
  946. X
  947. X/*
  948. X * for strict to work, legal options must be first
  949. X *
  950. X * Unfortunately, -a and -e are orthogonal to -c.
  951. X */
  952. X#define EXTENSIONS    8    /* where to clear */
  953. X#ifdef DEBUG
  954. Xchar awk_opts[] = "F:f:v:caeCVdD";
  955. X#else
  956. Xchar awk_opts[] = "F:f:v:caeCV";
  957. X#endif
  958. X
  959. Xint
  960. Xmain(argc, argv)
  961. Xint argc;
  962. Xchar **argv;
  963. X{
  964. X#ifdef DEBUG
  965. X    /* Print out the parse tree.   For debugging */
  966. X    register int dotree = 0;
  967. X#endif
  968. X    extern char *version_string;
  969. X    FILE *fp;
  970. X    int c;
  971. X    extern int opterr, optind;
  972. X    extern char *optarg;
  973. X     extern char *strrchr();
  974. X     extern char *tmpnam();
  975. X    extern SIGTYPE catchsig();
  976. X    int i;
  977. X    int nostalgia;
  978. X#ifdef somtime_in_the_future
  979. X    int regex_mode = RE_SYNTAX_POSIX_EGREP;
  980. X#else
  981. X    int regex_mode = RE_SYNTAX_AWK;
  982. X#endif
  983. X
  984. X    (void) signal(SIGFPE, catchsig);
  985. X    (void) signal(SIGSEGV, catchsig);
  986. X
  987. X    if (strncmp(version_string, "@(#)", 4) == 0)
  988. X        version_string += 4;
  989. X
  990. X    myname = strrchr(argv[0], '/');
  991. X    if (myname == NULL)
  992. X        myname = argv[0];
  993. X    else
  994. X        myname++;
  995. X    if (argc < 2)
  996. X        usage();
  997. X
  998. X    /* initialize the null string */
  999. X    Nnull_string = make_string("", 0);
  1000. X    Nnull_string->numbr = 0.0;
  1001. X    Nnull_string->type = Node_val;
  1002. X    Nnull_string->flags = (PERM|STR|NUM|NUMERIC);
  1003. X
  1004. X    /* Set up the special variables */
  1005. X
  1006. X    /*
  1007. X     * Note that this must be done BEFORE arg parsing else -F
  1008. X     * breaks horribly 
  1009. X     */
  1010. X    init_vars();
  1011. X
  1012. X    /* worst case */
  1013. X    emalloc(sourcefile, char **, argc * sizeof(char *), "main");
  1014. X
  1015. X
  1016. X#ifdef STRICT    /* strict new awk compatibility */
  1017. X    strict = 1;
  1018. X    awk_opts[EXTENSIONS] = '\0';
  1019. X#endif
  1020. X
  1021. X#ifndef STRICT
  1022. X    /* undocumented feature, inspired by nostalgia, and a T-shirt */
  1023. X    nostalgia = 0;
  1024. X    for (i = 1; i < argc && argv[i][0] == '-'; i++) {
  1025. X        if (argv[i][1] == '-')        /* -- */
  1026. X            break;
  1027. X        else if (argv[i][1] == 'c') {    /* compatibility mode */
  1028. X            nostalgia = 0;
  1029. X            break;
  1030. X        } else if (STREQ(&argv[i][1], "nostalgia"))
  1031. X            nostalgia = 1;
  1032. X            /* keep looping, in case -c after -nostalgia */
  1033. X    }
  1034. X    if (nostalgia) {
  1035. X        fprintf (stderr, "awk: bailing out near line 1\n");
  1036. X        abort();
  1037. X    }
  1038. X#endif
  1039. X        
  1040. X    while ((c = getopt (argc, argv, awk_opts)) != EOF) {
  1041. X        switch (c) {
  1042. X#ifdef DEBUG
  1043. X        case 'd':
  1044. X            debugging++;
  1045. X            dotree++;
  1046. X            break;
  1047. X
  1048. X        case 'D':
  1049. X            debugging++;
  1050. X            yydebug = 2;
  1051. X            break;
  1052. X#endif
  1053. X
  1054. X#ifndef STRICT
  1055. X        case 'c':
  1056. X            strict = 1;
  1057. X            break;
  1058. X#endif
  1059. X
  1060. X        case 'F':
  1061. X            set_fs(optarg);
  1062. X            break;
  1063. X
  1064. X        case 'f':
  1065. X            /*
  1066. X             * a la MKS awk, allow multiple -f options.
  1067. X             * this makes function libraries real easy.
  1068. X             * most of the magic is in the scanner.
  1069. X             */
  1070. X            sourcefile[++numfiles] = optarg;
  1071. X            break;
  1072. X
  1073. X        case 'v':
  1074. X            pre_assign(optarg);
  1075. X            break;
  1076. X
  1077. X        case 'V':
  1078. X            fprintf(stderr, "%s, patchlevel %d\n",
  1079. X                    version_string, PATCHLEVEL);
  1080. X            break;
  1081. X
  1082. X        case 'C':
  1083. X            copyleft();
  1084. X            break;
  1085. X
  1086. X        case 'a':    /* use old fashioned awk regexps */
  1087. X            regex_mode = RE_SYNTAX_AWK;
  1088. X            break;
  1089. X
  1090. X        case 'e':    /* use egrep style regexps, per Posix */
  1091. X            regex_mode = RE_SYNTAX_POSIX_EGREP;
  1092. X            break;
  1093. X
  1094. X        case '?':
  1095. X        default:
  1096. X            /* getopt will print a message for us */
  1097. X            /* S5R4 awk ignores bad options and keeps going */
  1098. X            break;
  1099. X        }
  1100. X    }
  1101. X
  1102. X    /* Tell the regex routines how they should work. . . */
  1103. X    (void) re_set_syntax(regex_mode);
  1104. X
  1105. X#ifdef DEBUG
  1106. X    setbuf(stdout, (char *) NULL);    /* make debugging easier */
  1107. X#endif
  1108. X    if (isatty(fileno(stdout)))
  1109. X        output_is_tty = 1;
  1110. X    /* No -f option, use next arg */
  1111. X    /* write to temp file and save sourcefile name */
  1112. X    if (numfiles == -1) {
  1113. X        int i;
  1114. X
  1115. X        if (optind > argc - 1)    /* no args left */
  1116. X            usage();
  1117. X        numfiles++;
  1118. X        i = strlen (argv[optind]);
  1119. X        if (i == 0) {    /* sanity check */
  1120. X            fprintf(stderr, "%s: empty program text\n", myname);
  1121. X            usage();
  1122. X            /* NOTREACHED */
  1123. X        }
  1124. X        sourcefile[0] = tmpnam((char *) NULL);
  1125. X        if ((fp = fopen (sourcefile[0], "w")) == NULL)
  1126. X            fatal("could not save source prog in temp file (%s)",
  1127. X            strerror(errno));
  1128. X        if (fwrite (argv[optind], 1, i, fp) == 0)
  1129. X            fatal(
  1130. X            "could not write source program to temp file (%s)",
  1131. X            strerror(errno));
  1132. X        if (argv[optind][i-1] != '\n')
  1133. X            putc ('\n', fp);
  1134. X        (void) fclose (fp);
  1135. X        tempsource++;
  1136. X        optind++;
  1137. X    }
  1138. X    init_args(optind, argc, myname, argv);
  1139. X
  1140. X    /* Read in the program */
  1141. X    if (yyparse() || errcount)
  1142. X        exit(1);
  1143. X
  1144. X#ifdef DEBUG
  1145. X    if (dotree)
  1146. X        print_parse_tree(expression_value);
  1147. X#endif
  1148. X    /* Set up the field variables */
  1149. X    init_fields();
  1150. X
  1151. X    if (begin_block)
  1152. X        (void) interpret(begin_block);
  1153. X    if (!exiting && (expression_value || end_block))
  1154. X        do_input();
  1155. X    if (end_block)
  1156. X        (void) interpret(end_block);
  1157. X    if (close_io() != 0 && exit_val == 0)
  1158. X        exit_val = 1;
  1159. X    exit(exit_val);
  1160. X    /* NOTREACHED */
  1161. X    return exit_val;
  1162. X}
  1163. X
  1164. Xstatic void
  1165. Xusage()
  1166. X{
  1167. X    char *opt1 = " -f progfile [--]";
  1168. X    char *opt2 = " [--] 'program'";
  1169. X#ifdef STRICT
  1170. X    char *regops = " [-ae] [-F fs] [-v var=val]"
  1171. X#else
  1172. X    char *regops = " [-aecCV] [-F fs] [-v var=val]";
  1173. X#endif
  1174. X
  1175. X    fprintf(stderr, "usage: %s%s%s file ...\n       %s%s%s file ...\n",
  1176. X        myname, regops, opt1, myname, regops, opt2);
  1177. X    exit(11);
  1178. X}
  1179. X
  1180. X/* Generate compiled regular expressions */
  1181. Xstruct re_pattern_buffer *
  1182. Xmake_regexp(s, ignorecase)
  1183. XNODE *s;
  1184. Xint ignorecase;
  1185. X{
  1186. X    struct re_pattern_buffer *rp;
  1187. X    char *err;
  1188. X
  1189. X    emalloc(rp, struct re_pattern_buffer *, sizeof(*rp), "make_regexp");
  1190. X    memset((char *) rp, 0, sizeof(*rp));
  1191. X    emalloc(rp->buffer, char *, 16, "make_regexp");
  1192. X    rp->allocated = 16;
  1193. X    emalloc(rp->fastmap, char *, 256, "make_regexp");
  1194. X
  1195. X    if (! strict && ignorecase)
  1196. X        rp->translate = casetable;
  1197. X    else
  1198. X        rp->translate = NULL;
  1199. X    if ((err = re_compile_pattern(s->stptr, s->stlen, rp)) != NULL)
  1200. X        fatal("%s: /%s/", err, s->stptr);
  1201. X    free_temp(s);
  1202. X    return rp;
  1203. X}
  1204. X
  1205. Xstruct re_pattern_buffer *
  1206. Xmk_re_parse(s, ignorecase)
  1207. Xchar *s;
  1208. Xint ignorecase;
  1209. X{
  1210. X    char *src;
  1211. X    register char *dest;
  1212. X    register int c;
  1213. X    int in_brack = 0;
  1214. X
  1215. X    for (dest = src = s; *src != '\0';) {
  1216. X        if (*src == '\\') {
  1217. X            c = *++src;
  1218. X            switch (c) {
  1219. X            case '/':
  1220. X            case 'a':
  1221. X            case 'b':
  1222. X            case 'f':
  1223. X            case 'n':
  1224. X            case 'r':
  1225. X            case 't':
  1226. X            case 'v':
  1227. X            case 'x':
  1228. X            case '0':
  1229. X            case '1':
  1230. X            case '2':
  1231. X            case '3':
  1232. X            case '4':
  1233. X            case '5':
  1234. X            case '6':
  1235. X            case '7':
  1236. X                c = parse_escape(&src);
  1237. X                if (c < 0)
  1238. X                    cant_happen();
  1239. X                *dest++ = (char)c;
  1240. X                break;
  1241. X            default:
  1242. X                *dest++ = '\\';
  1243. X                *dest++ = (char)c;
  1244. X                src++;
  1245. X                break;
  1246. X            }
  1247. X        } else if (*src == '/' && ! in_brack)
  1248. X            break;
  1249. X        else {
  1250. X            if (*src == '[')
  1251. X                in_brack = 1;
  1252. X            else if (*src == ']')
  1253. X                in_brack = 0;
  1254. X
  1255. X            *dest++ = *src++;
  1256. X        }
  1257. X    }
  1258. X    return make_regexp(tmp_string(s, dest-s), ignorecase);
  1259. X}
  1260. X
  1261. Xstatic void
  1262. Xcopyleft ()
  1263. X{
  1264. X    extern char *version_string;
  1265. X    char *cp;
  1266. X    static char blurb[] =
  1267. X"Copyright (C) 1989, Free Software Foundation.\n\
  1268. XGNU Awk comes with ABSOLUTELY NO WARRANTY.  This is free software, and\n\
  1269. Xyou are welcome to distribute it under the terms of the GNU General\n\
  1270. XPublic License, which covers both the warranty information and the\n\
  1271. Xterms for redistribution.\n\n\
  1272. XYou should have received a copy of the GNU General Public License along\n\
  1273. Xwith this program; if not, write to the Free Software Foundation, Inc.,\n\
  1274. X675 Mass Ave, Cambridge, MA 02139, USA.\n";
  1275. X
  1276. X    fprintf (stderr, "%s, patchlevel %d\n", version_string, PATCHLEVEL);
  1277. X    fputs(blurb, stderr);
  1278. X    fflush(stderr);
  1279. X}
  1280. X
  1281. Xstatic void
  1282. Xset_fs(str)
  1283. Xchar *str;
  1284. X{
  1285. X    register NODE **tmp;
  1286. X
  1287. X    tmp = get_lhs(FS_node, 0);
  1288. X    /*
  1289. X     * Only if in full compatibility mode check for the stupid special
  1290. X     * case so -F\t works as documented in awk even though the shell
  1291. X     * hands us -Ft.  Bleah!
  1292. X     */
  1293. X    if (strict && str[0] == 't' && str[1] == '\0')
  1294. X        str[0] = '\t';
  1295. X    *tmp = make_string(str, 1);
  1296. X    do_deref();
  1297. X}
  1298. X
  1299. Xstatic void
  1300. Xinit_args(argc0, argc, argv0, argv)
  1301. Xint argc0, argc;
  1302. Xchar *argv0;
  1303. Xchar **argv;
  1304. X{
  1305. X    int i, j;
  1306. X    NODE **aptr;
  1307. X
  1308. X    ARGV_node = spc_var("ARGV", Nnull_string);
  1309. X    aptr = assoc_lookup(ARGV_node, tmp_number(0.0));
  1310. X    *aptr = make_string(argv0, strlen(argv0));
  1311. X    for (i = argc0, j = 1; i < argc; i++) {
  1312. X        aptr = assoc_lookup(ARGV_node, tmp_number((AWKNUM) j));
  1313. X        *aptr = make_string(argv[i], strlen(argv[i]));
  1314. X        j++;
  1315. X    }
  1316. X    ARGC_node = spc_var("ARGC", make_number((AWKNUM) j));
  1317. X}
  1318. X
  1319. X/*
  1320. X * Set all the special variables to their initial values.
  1321. X */
  1322. Xstatic void
  1323. Xinit_vars()
  1324. X{
  1325. X    extern char **environ;
  1326. X    char *var, *val;
  1327. X    NODE **aptr;
  1328. X    int i;
  1329. X
  1330. X    FS_node = spc_var("FS", make_string(" ", 1));
  1331. X    NF_node = spc_var("NF", make_number(-1.0));
  1332. X    RS_node = spc_var("RS", make_string("\n", 1));
  1333. X    NR_node = spc_var("NR", make_number(0.0));
  1334. X    FNR_node = spc_var("FNR", make_number(0.0));
  1335. X    FILENAME_node = spc_var("FILENAME", make_string("-", 1));
  1336. X    OFS_node = spc_var("OFS", make_string(" ", 1));
  1337. X    ORS_node = spc_var("ORS", make_string("\n", 1));
  1338. X    OFMT_node = spc_var("OFMT", make_string("%.6g", 4));
  1339. X    RLENGTH_node = spc_var("RLENGTH", make_number(0.0));
  1340. X    RSTART_node = spc_var("RSTART", make_number(0.0));
  1341. X    SUBSEP_node = spc_var("SUBSEP", make_string("\034", 1));
  1342. X    IGNORECASE_node = spc_var("IGNORECASE", make_number(0.0));
  1343. X
  1344. X    ENVIRON_node = spc_var("ENVIRON", Nnull_string);
  1345. X    for (i = 0; environ[i]; i++) {
  1346. X        static char nullstr[] = "";
  1347. X
  1348. X        var = environ[i];
  1349. X        val = strchr(var, '=');
  1350. X        if (val)
  1351. X            *val++ = '\0';
  1352. X        else
  1353. X            val = nullstr;
  1354. X        aptr = assoc_lookup(ENVIRON_node, tmp_string(var, strlen (var)));
  1355. X        *aptr = make_string(val, strlen (val));
  1356. X
  1357. X        /* restore '=' so that system() gets a valid environment */
  1358. X        if (val != nullstr)
  1359. X            *--val = '=';
  1360. X    }
  1361. X}
  1362. X
  1363. X/* Create a special variable */
  1364. Xstatic NODE *
  1365. Xspc_var(name, value)
  1366. Xchar *name;
  1367. XNODE *value;
  1368. X{
  1369. X    register NODE *r;
  1370. X
  1371. X    if ((r = lookup(variables, name)) == NULL)
  1372. X        r = install(variables, name, node(value, Node_var, (NODE *) NULL));
  1373. X    return r;
  1374. X}
  1375. X
  1376. Xstatic void
  1377. Xpre_assign(v)
  1378. Xchar *v;
  1379. X{
  1380. X    char *cp;
  1381. X
  1382. X    cp = strchr(v, '=');
  1383. X    if (cp != NULL) {
  1384. X        *cp++ = '\0';
  1385. X        variable(v)->var_value = make_string(cp, strlen(cp));
  1386. X    } else {
  1387. X        fprintf (stderr,
  1388. X            "%s: '%s' argument to -v not in 'var=value' form\n",
  1389. X                myname, v);
  1390. X        usage();
  1391. X    }
  1392. X}
  1393. X
  1394. XSIGTYPE
  1395. Xcatchsig(sig, code)
  1396. Xint sig, code;
  1397. X{
  1398. X#ifdef lint
  1399. X    code = 0; sig = code; code = sig;
  1400. X#endif
  1401. X    if (sig == SIGFPE) {
  1402. X        fatal("floating point exception");
  1403. X    } else if (sig == SIGSEGV) {
  1404. X        msg("fatal error: segmentation fault");
  1405. X        /* fatal won't abort() if not compiled for debugging */
  1406. X        abort();
  1407. X    } else
  1408. X        cant_happen();
  1409. X    /* NOTREACHED */
  1410. X}
  1411. END_OF_FILE
  1412.   if test 12595 -ne `wc -c <'./main.c'`; then
  1413.     echo shar: \"'./main.c'\" unpacked with wrong size!
  1414.   fi
  1415.   # end of './main.c'
  1416. fi
  1417. if test -f './regex.h' -a "${1}" != "-c" ; then 
  1418.   echo shar: Will not clobber existing file \"'./regex.h'\"
  1419. else
  1420.   echo shar: Extracting \"'./regex.h'\" \(12788 characters\)
  1421.   sed "s/^X//" >'./regex.h' <<'END_OF_FILE'
  1422. X/* Definitions for data structures callers pass the regex library.
  1423. X   Copyright (C) 1985 Free Software Foundation, Inc.
  1424. X
  1425. X               NO WARRANTY
  1426. X
  1427. X  BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  1428. XNO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  1429. XWHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  1430. XRICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  1431. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  1432. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1433. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  1434. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  1435. XDEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  1436. XCORRECTION.
  1437. X
  1438. X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  1439. XSTALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  1440. XWHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  1441. XLIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  1442. XOTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  1443. XUSE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  1444. XDATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  1445. XA FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  1446. XPROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  1447. XDAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  1448. X
  1449. X        GENERAL PUBLIC LICENSE TO COPY
  1450. X
  1451. X  1. You may copy and distribute verbatim copies of this source file
  1452. Xas you receive it, in any medium, provided that you conspicuously and
  1453. Xappropriately publish on each copy a valid copyright notice "Copyright
  1454. X(C) 1985 Free Software Foundation, Inc."; and include following the
  1455. Xcopyright notice a verbatim copy of the above disclaimer of warranty
  1456. Xand of this License.  You may charge a distribution fee for the
  1457. Xphysical act of transferring a copy.
  1458. X
  1459. X  2. You may modify your copy or copies of this source file or
  1460. Xany portion of it, and copy and distribute such modifications under
  1461. Xthe terms of Paragraph 1 above, provided that you also do the following:
  1462. X
  1463. X    a) cause the modified files to carry prominent notices stating
  1464. X    that you changed the files and the date of any change; and
  1465. X
  1466. X    b) cause the whole of any work that you distribute or publish,
  1467. X    that in whole or in part contains or is a derivative of this
  1468. X    program or any part thereof, to be licensed at no charge to all
  1469. X    third parties on terms identical to those contained in this
  1470. X    License Agreement (except that you may choose to grant more extensive
  1471. X    warranty protection to some or all third parties, at your option).
  1472. X
  1473. X    c) You may charge a distribution fee for the physical act of
  1474. X    transferring a copy, and you may at your option offer warranty
  1475. X    protection in exchange for a fee.
  1476. X
  1477. XMere aggregation of another unrelated program with this program (or its
  1478. Xderivative) on a volume of a storage or distribution medium does not bring
  1479. Xthe other program under the scope of these terms.
  1480. X
  1481. X  3. You may copy and distribute this program (or a portion or derivative
  1482. Xof it, under Paragraph 2) in object code or executable form under the terms
  1483. Xof Paragraphs 1 and 2 above provided that you also do one of the following:
  1484. X
  1485. X    a) accompany it with the complete corresponding machine-readable
  1486. X    source code, which must be distributed under the terms of
  1487. X    Paragraphs 1 and 2 above; or,
  1488. X
  1489. X    b) accompany it with a written offer, valid for at least three
  1490. X    years, to give any third party free (except for a nominal
  1491. X    shipping charge) a complete machine-readable copy of the
  1492. X    corresponding source code, to be distributed under the terms of
  1493. X    Paragraphs 1 and 2 above; or,
  1494. X
  1495. X    c) accompany it with the information you received as to where the
  1496. X    corresponding source code may be obtained.  (This alternative is
  1497. X    allowed only for noncommercial distribution and only if you
  1498. X    received the program in object code or executable form alone.)
  1499. X
  1500. XFor an executable file, complete source code means all the source code for
  1501. Xall modules it contains; but, as a special exception, it need not include
  1502. Xsource code for modules which are standard libraries that accompany the
  1503. Xoperating system on which the executable file runs.
  1504. X
  1505. X  4. You may not copy, sublicense, distribute or transfer this program
  1506. Xexcept as expressly provided under this License Agreement.  Any attempt
  1507. Xotherwise to copy, sublicense, distribute or transfer this program is void and
  1508. Xyour rights to use the program under this License agreement shall be
  1509. Xautomatically terminated.  However, parties who have received computer
  1510. Xsoftware programs from you with this License Agreement will not have
  1511. Xtheir licenses terminated so long as such parties remain in full compliance.
  1512. X
  1513. X  5. If you wish to incorporate parts of this program into other free
  1514. Xprograms whose distribution conditions are different, write to the Free
  1515. XSoftware Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  1516. Xworked out a simple rule that can be stated here, but we will often permit
  1517. Xthis.  We will be guided by the two goals of preserving the free status of
  1518. Xall derivatives of our free software and of promoting the sharing and reuse of
  1519. Xsoftware.
  1520. X
  1521. X
  1522. XIn other words, you are welcome to use, share and improve this program.
  1523. XYou are forbidden to forbid anyone else to use, share and improve
  1524. Xwhat you give them.   Help stamp out software-hoarding!  */
  1525. X
  1526. X
  1527. X/* Define number of parens for which we record the beginnings and ends.
  1528. X   This affects how much space the `struct re_registers' type takes up.  */
  1529. X#ifndef RE_NREGS
  1530. X#define RE_NREGS 10
  1531. X#endif
  1532. X
  1533. X/* These bits are used in the obscure_syntax variable to choose among
  1534. X   alternative regexp syntaxes.  */
  1535. X
  1536. X/* 1 means plain parentheses serve as grouping, and backslash
  1537. X     parentheses are needed for literal searching.
  1538. X   0 means backslash-parentheses are grouping, and plain parentheses
  1539. X     are for literal searching.  */
  1540. X#define RE_NO_BK_PARENS 1
  1541. X
  1542. X/* 1 means plain | serves as the "or"-operator, and \| is a literal.
  1543. X   0 means \| serves as the "or"-operator, and | is a literal.  */
  1544. X#define RE_NO_BK_VBAR 2
  1545. X
  1546. X/* 0 means plain + or ? serves as an operator, and \+, \? are literals.
  1547. X   1 means \+, \? are operators and plain +, ? are literals.  */
  1548. X#define RE_BK_PLUS_QM 4
  1549. X
  1550. X/* 1 means | binds tighter than ^ or $.
  1551. X   0 means the contrary.  */
  1552. X#define RE_TIGHT_VBAR 8
  1553. X
  1554. X/* 1 means treat \n as an _OR operator
  1555. X   0 means treat it as a normal character */
  1556. X#define RE_NEWLINE_OR 16
  1557. X
  1558. X/* 0 means that a special characters (such as *, ^, and $) always have
  1559. X     their special meaning regardless of the surrounding context.
  1560. X   1 means that special characters may act as normal characters in some
  1561. X     contexts.  Specifically, this applies to:
  1562. X    ^ - only special at the beginning, or after ( or |
  1563. X    $ - only special at the end, or before ) or |
  1564. X    *, +, ? - only special when not after the beginning, (, or | */
  1565. X#define RE_CONTEXT_INDEP_OPS 32
  1566. X
  1567. X/* 0 means that \ before anything inside [ and ] is taken as a real \.
  1568. X   1 means that such a \ escapes the following character  This is a
  1569. X   special case for AWK. */
  1570. X#define RE_AWK_CLASS_HACK 64
  1571. X
  1572. X/* Now define combinations of bits for the standard possibilities.  */
  1573. X#define RE_SYNTAX_POSIX_EGREP (RE_NO_BK_PARENS | RE_NO_BK_VBAR \
  1574. X            | RE_CONTEXT_INDEP_OPS)
  1575. X#define RE_SYNTAX_AWK (RE_SYNTAX_POSIX_EGREP | RE_AWK_CLASS_HACK)
  1576. X#define RE_SYNTAX_EGREP (RE_SYNTAX_POSIX_EGREP | RE_NEWLINE_OR)
  1577. X#define RE_SYNTAX_GREP (RE_BK_PLUS_QM | RE_NEWLINE_OR)
  1578. X#define RE_SYNTAX_EMACS 0
  1579. X
  1580. X/* This data structure is used to represent a compiled pattern. */
  1581. X
  1582. Xstruct re_pattern_buffer
  1583. X  {
  1584. X    char *buffer;    /* Space holding the compiled pattern commands. */
  1585. X    int allocated;    /* Size of space that  buffer  points to */
  1586. X    int used;        /* Length of portion of buffer actually occupied */
  1587. X    char *fastmap;    /* Pointer to fastmap, if any, or zero if none. */
  1588. X            /* re_search uses the fastmap, if there is one,
  1589. X               to skip quickly over totally implausible characters */
  1590. X    char *translate;    /* Translate table to apply to all characters before comparing.
  1591. X               Or zero for no translation.
  1592. X               The translation is applied to a pattern when it is compiled
  1593. X               and to data when it is matched. */
  1594. X    char fastmap_accurate;
  1595. X            /* Set to zero when a new pattern is stored,
  1596. X               set to one when the fastmap is updated from it. */
  1597. X    char can_be_null;   /* Set to one by compiling fastmap
  1598. X               if this pattern might match the null string.
  1599. X               It does not necessarily match the null string
  1600. X               in that case, but if this is zero, it cannot.
  1601. X               2 as value means can match null string
  1602. X               but at end of range or before a character
  1603. X               listed in the fastmap.  */
  1604. X  };
  1605. X
  1606. X/* Structure to store "register" contents data in.
  1607. X
  1608. X   Pass the address of such a structure as an argument to re_match, etc.,
  1609. X   if you want this information back.
  1610. X
  1611. X   start[i] and end[i] record the string matched by \( ... \) grouping i,
  1612. X   for i from 1 to RE_NREGS - 1.
  1613. X   start[0] and end[0] record the entire string matched. */
  1614. X
  1615. Xstruct re_registers
  1616. X  {
  1617. X    int start[RE_NREGS];
  1618. X    int end[RE_NREGS];
  1619. X  };
  1620. X
  1621. X/* These are the command codes that appear in compiled regular expressions, one per byte.
  1622. X  Some command codes are followed by argument bytes.
  1623. X  A command code can specify any interpretation whatever for its arguments.
  1624. X  Zero-bytes may appear in the compiled regular expression. */
  1625. X
  1626. Xenum regexpcode
  1627. X  {
  1628. X    unused,
  1629. X    exactn,    /* followed by one byte giving n, and then by n literal bytes */
  1630. X    begline,   /* fails unless at beginning of line */
  1631. X    endline,   /* fails unless at end of line */
  1632. X    jump,     /* followed by two bytes giving relative address to jump to */
  1633. X    on_failure_jump,     /* followed by two bytes giving relative address of place
  1634. X                    to resume at in case of failure. */
  1635. X    finalize_jump,     /* Throw away latest failure point and then jump to address. */
  1636. X    maybe_finalize_jump, /* Like jump but finalize if safe to do so.
  1637. X                This is used to jump back to the beginning
  1638. X                of a repeat.  If the command that follows
  1639. X                this jump is clearly incompatible with the
  1640. X                one at the beginning of the repeat, such that
  1641. X                we can be sure that there is no use backtracking
  1642. X                out of repetitions already completed,
  1643. X                then we finalize. */
  1644. X    dummy_failure_jump,  /* jump, and push a dummy failure point.
  1645. X                This failure point will be thrown away
  1646. X                if an attempt is made to use it for a failure.
  1647. X                A + construct makes this before the first repeat.  */
  1648. X    anychar,     /* matches any one character */
  1649. X    charset,     /* matches any one char belonging to specified set.
  1650. X            First following byte is # bitmap bytes.
  1651. X            Then come bytes for a bit-map saying which chars are in.
  1652. X            Bits in each byte are ordered low-bit-first.
  1653. X            A character is in the set if its bit is 1.
  1654. X            A character too large to have a bit in the map
  1655. X            is automatically not in the set */
  1656. X    charset_not, /* similar but match any character that is NOT one of those specified */
  1657. X    start_memory, /* starts remembering the text that is matched
  1658. X            and stores it in a memory register.
  1659. X            followed by one byte containing the register number.
  1660. X            Register numbers must be in the range 0 through NREGS. */
  1661. X    stop_memory, /* stops remembering the text that is matched
  1662. X            and stores it in a memory register.
  1663. X            followed by one byte containing the register number.
  1664. X            Register numbers must be in the range 0 through NREGS. */
  1665. X    duplicate,    /* match a duplicate of something remembered.
  1666. X            Followed by one byte containing the index of the memory register. */
  1667. X    before_dot,     /* Succeeds if before dot */
  1668. X    at_dot,     /* Succeeds if at dot */
  1669. X    after_dot,     /* Succeeds if after dot */
  1670. X    begbuf,      /* Succeeds if at beginning of buffer */
  1671. X    endbuf,      /* Succeeds if at end of buffer */
  1672. X    wordchar,    /* Matches any word-constituent character */
  1673. X    notwordchar, /* Matches any char that is not a word-constituent */
  1674. X    wordbeg,     /* Succeeds if at word beginning */
  1675. X    wordend,     /* Succeeds if at word end */
  1676. X    wordbound,   /* Succeeds if at a word boundary */
  1677. X    notwordbound, /* Succeeds if not at a word boundary */
  1678. X    syntaxspec,  /* Matches any character whose syntax is specified.
  1679. X            followed by a byte which contains a syntax code, Sword or such like */
  1680. X    notsyntaxspec /* Matches any character whose syntax differs from the specified. */
  1681. X  };
  1682. X
  1683. Xextern char *re_compile_pattern ();
  1684. X/* Is this really advertised? */
  1685. Xextern void re_compile_fastmap ();
  1686. Xextern int re_search (), re_search_2 ();
  1687. Xextern int re_match (), re_match_2 ();
  1688. X
  1689. X/* 4.2 bsd compatibility (yuck) */
  1690. Xextern char *re_comp ();
  1691. Xextern int re_exec ();
  1692. X
  1693. X#ifdef SYNTAX_TABLE
  1694. Xextern char *re_syntax_table;
  1695. X#endif
  1696. END_OF_FILE
  1697.   if test 12788 -ne `wc -c <'./regex.h'`; then
  1698.     echo shar: \"'./regex.h'\" unpacked with wrong size!
  1699.   fi
  1700.   # end of './regex.h'
  1701. fi
  1702. echo shar: End of archive 14 \(of 16\).
  1703. cp /dev/null ark14isdone
  1704. MISSING=""
  1705. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ; do
  1706.     if test ! -f ark${I}isdone ; then
  1707.     MISSING="${MISSING} ${I}"
  1708.     fi
  1709. done
  1710. if test "${MISSING}" = "" ; then
  1711.     echo You have unpacked all 16 archives.
  1712.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1713. else
  1714.     echo You still must unpack the following archives:
  1715.     echo "        " ${MISSING}
  1716. fi
  1717. exit 0
  1718. exit 0 # Just in case...
  1719.